<?php
class IndexController extends Yaf_Controller_Abstract {
    public function indexAction(){
        $this->redirect('register');
    }
    public function registerAction(){}
    public function acceptAction(){
        // Yaf_Dispatcher::getInstance()->disableView();
        $name = Request::aspost(6,'name','');
        $password = Request::aspost(6,'password','');
        
        if(empty($name) || empty($password)){
            return false;
        }else{
            $name = Request::val($name,5);
            $password = Request::val($password,7);
            $data['name'] = $name;
            $salt = mcrypt_create_iv(22, MCRYPT_DEV_URANDOM);
            $options = [
                'cost' => 11,
                'salt' => $salt,
            ];
            $data['password'] = password_hash($password,PASSWORD_BCRYPT,$options);
            $data['ip'] = Request::ip();
            $model = new MemberModel;
            $flag = $model->insert($data);
            $this->_view->name = $name;
            if(empty($flag)){
                $this->_view->log = "error";
            }else{
                $this->_view->log = "success";
            }
        }
    }
    public function signAction(){
        $isGet = $this->_request->isGet();
        if($isGet){
            return true;
        }else{
            $verify = Request::aspost(6,'verifycode',null);
            $name = Request::aspost(6,'name',null);
            $password = Request::aspost(6,'password',null);
            if($verify == null || $name == null || $password == null){
                $warning = '请填上用户名，密码，和验证码';
                $this->_view->warning= $warning;
            }else{
                $verifyObject = new Verify_Code;
                $flag = $verifyObject->check($verify,'3');
                if($flag){
                    $mData = Db::table('member')->where(['name' => $name])->one();
                    $model= new MemberModel;
                    if(empty($mData)){
                        $this->_view->warning = 'The User Name Is Wrong';
                    }else{
                        $flag = password_verify($password,$mData['password']);
                        if(true == $flag){
                            $member = [
                                'id' =>  $mData['id'],
                                'name' =>  $mData['name'],
                                'ip' => Request::ip(),
                            ];
                            $session = Yaf_Session::getInstance();
                            $session->set('info', $member);
                            $this->redirect('/member/center/index');                        
                        }else{
                            $this->_view->warning =  ' Password Code Is Wrong';
                        }
                    }
                }else{
                    $this->_view->warning = 'The Verify Code Is Wrong';
                }
            }
        }
    }
    public function verifyAction(){
        $verify = new Verify_Code;
        $verify->execute('3');
    }
    public function logoutAction(){
        if($this->_request->isGet()){
            $mid = Request::asget(3,id);
            $session = Yaf_Session::getInstance();
            $sign = 'member'.$mid;
            if($session->has($sign)){
                $session->del($sign);
                $this->redirect('sign');
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
}